package com.ruoyi.syrw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import com.ruoyi.syrw.domain.bo.BiExcelTypeBo;
import com.ruoyi.syrw.domain.vo.BiExcelTypeVo;
import com.ruoyi.syrw.domain.BiExcelType;
import com.ruoyi.syrw.mapper.BiExcelTypeMapper;
import com.ruoyi.syrw.service.IBiExcelTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * Excel类型管理Service业务层处理
 *
 * @author zyb
 * @date 2024-04-30
 */
@RequiredArgsConstructor
@Service
public class BiExcelTypeServiceImpl implements IBiExcelTypeService {

    private final BiExcelTypeMapper baseMapper;

    /**
     * 查询Excel类型管理
     */
    @Override
    public BiExcelTypeVo queryById(Long excelTypeId){
        return baseMapper.selectVoById(excelTypeId);
    }

    /**
     * 查询Excel类型管理列表
     */
    @Override
    public TableDataInfo<BiExcelTypeVo> queryPageList(BiExcelTypeBo bo, PageQuery pageQuery) {
        if (ObjUtil.isEmpty(bo.getSyTypeId())) {
            return TableDataInfo.build(Lists.newArrayList());
        }
        LambdaQueryWrapper<BiExcelType> lqw = buildQueryWrapper(bo);
        Page<BiExcelTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询Excel类型管理列表
     */
    @Override
    public List<BiExcelTypeVo> queryList(BiExcelTypeBo bo) {
        LambdaQueryWrapper<BiExcelType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BiExcelType> buildQueryWrapper(BiExcelTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BiExcelType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSyTypeId() != null, BiExcelType::getSyTypeId, bo.getSyTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getExcelTypeName()), BiExcelType::getExcelTypeName, bo.getExcelTypeName());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), BiExcelType::getLabel, bo.getLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getExcelPath()), BiExcelType::getExcelPath, bo.getExcelPath());
        lqw.eq(StringUtils.isNotBlank(bo.getCompontPath()), BiExcelType::getCompontPath, bo.getCompontPath());
        lqw.like(StringUtils.isNotBlank(bo.getExportFileName()), BiExcelType::getExportFileName, bo.getExportFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getExpandInfo()), BiExcelType::getExpandInfo, bo.getExpandInfo());
        lqw.orderByAsc(BiExcelType::getTypeSort);
        lqw.orderByAsc(BiExcelType::getLabel);
        lqw.orderByAsc(BiExcelType::getCreateTime);
        return lqw;
    }

    /**
     * 新增Excel类型管理
     */
    @Override
    public Boolean insertByBo(BiExcelTypeBo bo) {
        BiExcelType add = BeanUtil.toBean(bo, BiExcelType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelTypeId(add.getExcelTypeId());
        }
        return flag;
    }

    /**
     * 修改Excel类型管理
     */
    @Override
    public Boolean updateByBo(BiExcelTypeBo bo) {
        BiExcelType update = BeanUtil.toBean(bo, BiExcelType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BiExcelType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除Excel类型管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
